//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_chat_api_serv

import (
	"atomgit.com/openlinksaas/api-server/dao/chat_dao"
	"atomgit.com/openlinksaas/api-server/dao/project_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_chat_api"
)

const (
	_UPDATE_MSG_AGE = int64(3600 * 1000) //1个小时内可以修改消息
)

type ProjectChatApiImpl struct {
	project_chat_api.UnimplementedProjectChatApiServer
}

func (apiImpl *ProjectChatApiImpl) calcUserPerm(groupItem *chat_dao.ChatGroupInfoDbItem, userId string, projectItem *project_dao.ProjectInfoDbItem, roleItem *project_dao.RoleInfoDbItem) *project_chat_api.UserPerm {
	if groupItem.System {
		return &project_chat_api.UserPerm{
			CanChangeOwner: false,
			CanLeave:       false,
			CanUpdate:      false,
			CanRemove:      false,
		}
	}
	canChangeOwner := false
	canLeave := true
	canUpdate := false
	canRemove := false

	if groupItem.OwnerUserId == userId || projectItem.OwnerUserId == userId || roleItem.BasicInfo.Admin {
		canChangeOwner = true
		canUpdate = true
		canRemove = true
	}
	if groupItem.OwnerUserId == userId {
		canLeave = false
	}
	return &project_chat_api.UserPerm{
		CanChangeOwner: canChangeOwner,
		CanLeave:       canLeave,
		CanUpdate:      canUpdate,
		CanRemove:      canRemove,
	}
}
